<?php
class Penjual_langsung extends CI_Controller{
    var $_public_view;

    function __construct() {
        parent::__construct();
        $this->load->model('model_listing', 'mlisting');
        $this->load->helper('url','site');
        $this->load->helper('form');
        $this->load->library('session');
        $this->config->load('artyproperty');
        $this->_public_view = $this->config->item('public_view');
    }

    function index() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $data = array(
            'page_title' => 'Form Penjual Langsung',
            'page_content' => 'penjual_langsung',
            'error' => array(), 'isSubmit' => 'false',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

    function tambah(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }
        $error = array('email'=>'','hp'=>'','kosong'=>'');
        //ambil dari method POST
        $reg['titel'] = $this->input->post('titel',true);
        $reg['nama_depan'] = $this->input->post('nama_depan',true);
        $reg['nama_belakang'] = $this->input->post('nama_belakang',true);

        $email = $this->input->post('email',true);
        if($email != ""){
            $query_email = $this->db->query("SELECT COUNT(email) AS count FROM `penjual` WHERE email = '".$email."'");
            $row_email = $query_email->row();
            if($row_email->count >0){$error['email']="email sudah ada.";
            } else {
                $reg['email'] = $email;
            }
        } else {
            $error['kosong'] = $error['kosong']." email belum diisi.";
        }        

        $hp = $this->input->post('hp',true);
        if($hp != ""){
            $query_hp = $this->db->query("SELECT COUNT(hp) AS count FROM `penjual` WHERE hp = '".$hp."'");
            $row_hp = $query_hp->row();
            if($row_hp->count >0){$error['hp']="hp sudah ada.";
            } else {
                $reg['hp'] = $hp;
            }
        } else {
            $error['kosong'] = $error['kosong']." no hp belum diisi.";
        } 

        $reg['tgl_lahir'] = $this->input->post('tgl_lahir',true);
        $reg['status'] = $this->input->post('status',true);
        
        $tinggal_di = $this->input->post('tinggal_di',true);
        if($tinggal_di != ""){
            $reg['tinggal_di'] = $tinggal_di;
        } else {
            $error['kosong'] = $error['kosong']." lokasi tinggal belum diisi.";
        } 

        $cari_property = $this->input->post('cari_property',true);
        if($cari_property != 0){
            $reg['cari_property'] = $cari_property;
        } else {
            $error['kosong'] = $error['kosong']." silakan pilih kategori property.";
        } 

        $pendapatan = $this->input->post('pendapatan',true);
        if($pendapatan != 0){
            $reg['pendapatan'] = $pendapatan;
        } else {
            $error['kosong'] = $error['kosong']." silakan pilih pendapatan.";
        }

        $reg['mau_proyek'] = $this->input->post('mau_proyek',true);
        $reg['mau_info'] = $this->input->post('mau_info',true);

        if(sizeof($error)==0){
            $this->db->insert('penjual', $reg);
            $data = array(
                'error' => $error,
                'page_title' => 'Penjual Langsung',
                'page_content' => 'penjual_langsung',
                'isLogin' => $islogin,
                'user' => $user);
            $this->load->view($this->_public_view, $data);
        } else {
            $data = array(
                'error' => $error, 'isSubmit' => 'true',
                'page_title' => 'Penjual Langsung',
                'page_content' => 'penjual_langsung',
                'isLogin' => $islogin,
                'user' => $user);
            $this->load->view($this->_public_view, $data);
        }
    }

    /**
     * fungsi untuk mengecek duplikasi username pada database
     * @param <type> $username
     * @return <type>
     */
    public function spare_username($username){
        $sql = "SELECT username
                FROM user
                WHERE username=?";
        $query = $this->db->query($sql, array($username));
        $this->valid->set_message('spare_username',"Nama $username sudah ada. Silakan isi dengan username yg lain");
        return ($query->num_rows()==0)? TRUE : FALSE;

    }
}

?>
